package futian
{
	import flash.desktop.NativeApplication;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	import flash.utils.clearTimeout;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;

	[SWF(width="480",height="270",frameRate="12",backgroundColor=0xeeeeee)]
	//[SWF(width="1920",height="1080",frameRate="12",backgroundColor=0xeeeeee)]
		public class test extends Sprite
		{
			private static var _stageW:int;
			private static var _stageH:int;
			private static var _main:test;

			public static function get main():test
			{
				return _main;
			}
			public static function get stageW():int
			{
				return _stageW;
			}
			public static function get stageH():int
			{
				return _stageH;
			}
			public function test()
			{
				_main = this;

				//CONFIG::debugging
				/*
				{
					addChild(new XiangkuangSelect());
					return;
				}
				*/

				if(stage)init(null);
				else addEventListener(Event.ADDED_TO_STAGE,init);
			}

			private static var _startTime:uint;
			public static function get startTime():uint
			{
				return _startTime;
			}
			public static function get now():uint
			{
				return getTimer();
			}
			public static function get passedTime():uint
			{
				return now - startTime;
			}

			private function init(e:Event):void
			{
				addChild(new Authorize("47423847"));
				removeEventListener(Event.ADDED_TO_STAGE,init);
				//addChild(new logs);
				logs.adds(Datas.quitTime);
				_stageW = stage.stageWidth;
				_stageH = stage.stageHeight;


				SwfLoader.SwfLoad("futian/bg.jpg",loaded);
				//SwfLoader.SwfLoad("futian/asset/bg1.jpg",loaded1);

				var full:Boolean = true;
				CONFIG::debugging{ full=false; }
				//stage.align = StageAlign.topLeft;
				if(full){
					stage.scaleMode = StageScaleMode.EXACT_FIT;
					stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
					//stage.nativeWindow.alwaysInFront = true;
					//stage.displayState = StageDisplayState.NORMAL;
				}
				addChild(new Toast(0x0,0xf2f2f2));
			}

			public static function key_events(e:KeyboardEvent):void
			{
				switch(e.keyCode){
					case Keyboard.BACK:
					case Keyboard.ESCAPE:
					case Keyboard.F4:
					case Keyboard.TAB:
						e.preventDefault();
						logs.adds("ESCAPE");
						Toast.show("确认退出");
						quit();
						break;
				}
			}
			private static function quit():void
			{
				NativeApplication.nativeApplication.exit();
			}

			private var _bg:Bitmap = null;
			public function get bg():Bitmap
			{
				if(_bg == null)
					return new Bitmap(new BitmapData(1920,1800));
				return _bg;
			}
			private function loaded(e:Event):void
			{
				if(e && e.type == Event.COMPLETE)
				{
					_bg = e.target.content as Bitmap;
					if(_bg==null)
						logs.adds("null Bitmap");
				}else{
					logs.adds(e);
				}
				bg.width = stage.stageWidth;
				bg.height = stage.stageHeight;
				//Datas.init();

				stage.addEventListener(MouseEvent.MOUSE_UP,resetClock);
				stage.addEventListener(MouseEvent.MOUSE_DOWN,resetClock);
				stage.addEventListener(MouseEvent.MOUSE_MOVE,resetClock);
				NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, key_events);

				_startTime = now;
				FirstPage.show();
				//SignPage.show();
				return;
				if(Datas.willPlayPictures)
					Window2.show();
			}

			private var timeoutId:uint;
			private function resetClock(e:MouseEvent):void
			{
				clearTimeout(timeoutId);
				if(!Datas.willPlayPictures)
				{
					timeoutId = setTimeout(quit,Datas.quitTime*1000);
				}
			}

			public static function newBtn(s:String,fun:Function=null):TxtBtn
			{
				var btn:TxtBtn = new TxtBtn(s,fun);
				btn.setformat(new Rectangle(0,0,315,108),0xd20e0b,0xd20e0b,0xffffff,0xffffff,0xee9900);
				return btn;
			}
		}
}
